package com.deuwise.system.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.deuwise.common.utils.R;
import com.deuwise.common.utils.Result;
import com.deuwise.common.validator.ValidatorUtils;
import com.deuwise.common.validator.group.AddGroup;
import com.deuwise.system.entity.AppDo;
import com.deuwise.system.entity.ConfigPageDo;
import com.deuwise.system.service.AppDoService;
import com.deuwise.system.service.ConfigPageDoService;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;

@Api(tags = "前端配置模块", description = "前端配置模块")
@RequestMapping("api/configPageDo")
@RestController()
public class ConfigPageController extends AbstractController {

    @Autowired
    private ConfigPageDoService configDoService;

    @ApiOperation(value = "新增或者修改前端配置项")
    @PostMapping("/insertOrUpdateConfig")
    public R insertOrUpdateConfig(@RequestBody ConfigPageDo configDo){
        ValidatorUtils.validateEntity(configDo, AddGroup.class);
        boolean isSuccess = false;
        Wrapper<ConfigPageDo> appDoWrapper=new EntityWrapper<>();
        appDoWrapper.eq("key",configDo.getKey());
        appDoWrapper.eq("code",configDo.getCode());
        if(StringUtils.isNotBlank(configDo.getId())){
            if(StringUtils.isNotBlank(configDo.getId())) {
                appDoWrapper.ne("id", configDo.getId());
            }
        }
       Integer count= configDoService.selectCount(appDoWrapper);
        if(count>0){
            return R.error(200,"名称不能重复");
        }
        Wrapper<ConfigPageDo> wrapper=new EntityWrapper<>();
        wrapper.eq("key",configDo.getKey());
        wrapper.eq("code",configDo.getCode());
        ConfigPageDo configPageDo=configDoService.selectOne(wrapper);
        if(configPageDo!=null){
            configDo.setId(configPageDo.getId());
        }
        isSuccess = configDoService.insertOrUpdate(configDo);
        if (isSuccess) {
            return R.ok();
        } else {
            return R.error(200,"新增或者修改失败，请稍后再试!");
        }
    }

    @Autowired
    private AppDoService appDoService;

    @ApiOperation(value = "获取list", notes = "获取list")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "关键字", defaultValue = "1", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "子应用code", defaultValue = "1", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "currentPage", value = "开始页数", defaultValue = "1", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页多少条", defaultValue = "15", dataType = "int", paramType = "query")})
    public Result<Page<ConfigPageDo>> list(String key,String code
    ){
        Wrapper<ConfigPageDo> wrapper = new EntityWrapper<ConfigPageDo>();
            if(StringUtils.isNotBlank(key)){
                wrapper.like("key",key);
            }
        if(StringUtils.isNotBlank(code)){
            wrapper.like("code",code);
        }
        wrapper.orderBy("code",true);
        Page<ConfigPageDo> sysDictPage = configDoService.selectPage(getPage(ConfigPageDo.class),wrapper);
        for(ConfigPageDo configPageDo:sysDictPage.getRecords()){
            if(StringUtils.isNotBlank(configPageDo.getCode())) {
                Wrapper<AppDo> appDoEntityWrapper=new EntityWrapper<>();
                appDoEntityWrapper.like("code", configPageDo.getCode());
                AppDo appDo= appDoService.selectOne(appDoEntityWrapper);
                if(appDo!=null) {
                    configPageDo.setAppName(appDo.getName());
                }
            }
        }

        return Result.ok(sysDictPage);
    }
    @ApiOperation("删除记录")
    @DeleteMapping("/{id}")
    @RequiresPermissions("sys:config:delete")
    public R delete(@PathVariable("id") String id){
        boolean result=configDoService.deleteById(id);
        return R.ok(result);
    }

    @ApiOperation(value = "批量删除记录")
    @RequestMapping(value = "/deleteSelect", method = RequestMethod.GET)
    @RequiresPermissions("sys:config:delete")
    public  R deleteSelect(@ApiParam(value = "ids集合",name = "ids",required = true) @RequestParam String[] ids){
        try {
            if (ids != null && ids.length > 0) {
                configDoService.deleteBatchIds(Arrays.asList(ids));
            }
        }catch (Exception ex){
            return  R.error("删除失败");
        }
        return  R.ok();
    }

   /* @ApiOperation("修改配置项")
    @PutMapping()
    @RequiresPermissions("sys:config:update")
    public R updateConfig(@RequestBody ConfigPageDo configDo){

        boolean result= configDoService.updateById(configDo);
        if(result){
            return  R.ok();
        }else{
            return  R.error("修改失败");
        }
    }*/

}
